image_name         := my-clash
container_name     := my-clash
volume_home        := $(container_name)-home


version   := 2.3.2
src       := https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v$(version)/Clash.Verge_$(version)_amd64.deb
package   := ./distfiles/clash-verge_$(version)_amd64.deb
cmd       := /usr/bin/clash-verge


container_username := debian
session_dbus_dir   := $(shell echo $$DBUS_SESSION_BUS_ADDRESS | sed 's/unix:path=\(.*\),.*/\1/' | xargs dirname)



opts := -it -d --rm --name $(container_name) --privileged --network host
opts += -e DISPLAY=$$DISPLAY
opts += -e GDK_SCALE=2
opts += -e GDK_DPI_SCALE=0.5
opts += -e XMODIFIERS=@im=fcitx
opts += -e QT_IM_MODULE=fcitx
opts += -e GTK_IM_MODULE=fcitx
opts += -e QT_QPA_PLATFORMTHEME=qt5ct
opts += -e XDG_RUNTIME_DIR=$$XDG_RUNTIME_DIR
opts += -v $$XDG_RUNTIME_DIR:$$XDG_RUNTIME_DIR
opts += -v /dev:/dev
opts += -v /tmp:/tmp
#opts += -v $(session_dbus_dir):$(session_dbus_dir)
#opts += -v /tmp/.X11-unix:/tmp/.X11-unix
opts += -v /run/dbus:/run/dbus
opts += -v /etc/machine-id:/etc/machine-id
opts += -v $(volume_home):/home/$(container_username):rw
opts += -v $$HOME/.bashrc:/home/$(container_username)/.bashrc:ro
opts += -v $$HOME/.fonts:/home/$(container_username)/.fonts:ro
opts += -v $$HOME/.icons:/home/$(container_username)/.icons:ro
opts += -v $$HOME/.themes:/home/$(container_username)/.themes:ro
opts += -v $$HOME/.gtkrc-2.0:/home/$(container_username)/.gtkrc-2.0:ro
opts += -v $$HOME/.config/gtk-3.0:/home/$(container_username)/.config/gtk-3.0:ro
opts += -v $$HOME/.config/gtk-2.0:/home/$(container_username)/.config/gtk-2.0:ro



run: image volume
	@if [[ -n `docker container ls --filter name=$(container_name) --quiet` ]]; then \
		echo -e "\e[1;32mkilling $(container_name):v$(version)\e[0m"; \
		docker kill $(container_name) &> /dev/null; \
	fi
	@xhost + &> /dev/null
	@echo -e "\e[1;32mrunning $(container_name):v$(version)\e[0m"
	@docker run $(opts) $(image_name):$(version) $(cmd) &> /dev/null



image:
	@if [[ -z `docker images | grep $(image_name) | grep $(version)` ]];then \
		echo -e "\e[1;32mbuilding $(image_name):v$(version)\e[0m"; \
		if [[ ! -e $(package) ]]; then \
			curl -L $(src) -o $(package); \
		fi; \
		docker build . --network host --build-arg PACKAGE=$(package) -t $(image_name):$(version); \
	fi


volume:
	@if [[ -z `docker volume ls --filter name=$(volume_home) --quiet` ]];then \
		echo -e "\e[1;32mcreating volume: $(volume_home)\e[0m"; \
		docker volume create $(volume_home) &> /dev/null; \
	fi
